package com.tangkun.environment.adapter;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.tangkun.environment.R;
import com.tangkun.environment.activity.DeleteImageActivity;
import com.tangkun.environment.activity.ImageActivity;
import com.tangkun.environment.utils.Utils;

/**
 * @author tangkun
 * @ClassName: ImageFileAdapter
 * @Description: TODO(GirdView图片列表适配器(不含添加图片))
 * @date 2016年8月1日 下午1:37:03
 */
public class ImageFileNoAddAdapter extends BaseAdapter {
    private int REQUEST_CODE = 1;// REQUEST_CODE
    private Activity activity;
    private LayoutInflater layoutInflater;
    private int res;
    /**
     * 可以删除或仅仅是展示
     */
    private boolean canDelete = false;

    public ImageFileNoAddAdapter(Activity activity, int res) {
        this.activity = activity;
        this.layoutInflater = LayoutInflater.from(activity);
        this.res = res;
    }

    public int getREQUEST_CODE() {
        return REQUEST_CODE;
    }

    public void setREQUEST_CODE(int REQUEST_CODE) {
        this.REQUEST_CODE = REQUEST_CODE;
    }

    private List<File> fileList = new ArrayList<File>();

    public List<File> getFileList() {
        if (fileList == null) {
            fileList = new ArrayList<File>();
        }
        return fileList;
    }

    public void setFileList(List<File> fileList) {
        this.fileList.clear();
        this.fileList.addAll(fileList);
    }

    public boolean isCanDelete() {
        return canDelete;
    }

    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
    }

    @Override
    public int getCount() {
        return fileList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // return null;
        return fileList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    class ViewHolder {
        public ImageView imageView;
        public ImageView cameraImageView;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(res, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            holder.cameraImageView = (ImageView) convertView.findViewById(R.id.camera_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.cameraImageView.setVisibility(View.GONE);
        holder.imageView.setVisibility(View.VISIBLE);
        // 由于现在可以传原图回来，所以这里要显示缩略图，否则会内存溢出的，因此不能采用ImageLoader那种图片展示方式了
        holder.imageView.setImageBitmap(Utils.getImageThumbnail(getItem(position).toString(), 200, 200));
        // ImageLoader.getInstance().displayImage(Uri.fromFile(fileList.get(position)).toString(),
        // holder.imageView);

        // 点击查看大图
        holder.imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (canDelete) {// 可以删除
                    // 预览图片，可以删除
                    List<String> urlList = new ArrayList<String>();
                    if (fileList != null) {
                        for (int i = 0; i < fileList.size(); i++) {
//                            urlList.add(Uri.fromFile(fileList.get(i)).toString());
                            urlList.add(fileList.get(i).getPath());
                        }
                    }
                    Intent intent = new Intent(activity, DeleteImageActivity.class);
                    intent.putExtra("urlList", (Serializable) urlList);
                    intent.putExtra("fileList", (Serializable) fileList);// 传递文件的集合过去
                    intent.putExtra("index", position);
                    activity.startActivityForResult(intent, REQUEST_CODE);
                } else {
                    // 预览图片,不可以删除
                    List<String> urlList = new ArrayList<String>();
                    Intent intent = new Intent(activity, ImageActivity.class);
                    intent.putExtra("urlList", (Serializable) urlList);
                    intent.putExtra("index", position);
                    activity.startActivity(intent);
                }
            }
        });
        return convertView;
    }
}
